;
!function () {
    var layer = layui.layer,
        form = layui.form,
        laypage = layui.laypage;

    var status = null;

    layer.load(2);

    to_page(1, 10);

    // 请求分页数据
    function to_page(pageNum, pageSize) {
        $.ajax({
            url: "/appfeedback/",
            type: "post",
            data: {
                "pageNum": pageNum,
                "pageSize": pageSize,
                "status": status
            },
            async: false,
            success: function (result) {
                layer.closeAll('loading');
                if (result.totalRow == 0) {
                    // 提示没有数据
                    $("#content").html("<tr><td colspan='4' style='text-align:center;font-size: 18px;color:#FF6700;'>没有查询到任何数据哦~</td></tr>");
                    // 隐藏分页条
                    $("#page_info").hide();
                } else {
                    // 拼装分页总览信息并赋给页面
                    build_pageInfo(result);
                    // 拼装tr并赋给页面
                    build_table(result);
                    // 显示分页按钮
                    laypage.render({
                        elem: 'page_nav_div',
                        count: result.totalRow,
                        curr: result.pageNumber,
                        limit: result.pageSize,
                        theme: "#FF6700",
                        prev: "<em>«</em>",
                        next: "<em>»</em>",
                        layout: ['prev', 'page', 'next', 'limit', 'skip'],
                        jump: function (obj, first) {
                            if (!first) {
                                to_page(obj.curr, obj.limit);
                            }
                        }
                    });
                    // 显示分页条
                    $("#page_info").show();
                }
            }
        });
    }

    // 构建分页细节
    function build_pageInfo(pageInfo) {
        $("#pagination_overview").html(
            "当前第" + pageInfo.pageNumber + "页," +
            "共有" + pageInfo.totalPage +
            "页,总计" +
            pageInfo.totalRow + "条记录");
    }

    // 构建表格
    function build_table(page) {
        // 清空tbody
        $("#content").empty();
        // 遍历数据并累加
        $.each(page.list, function () {
            var operation = "";
            var realNameTd = $("<td></td>").append(this.columns.realName);
            var statusTd = $("<td></td>").append(this.columns.status);
            var createTimeTd = $("<td></td>").append(getMyDate(this.columns.createTime));
            if (this.columns.status == "已处理") {
                operation = "<td>" +
                    "<button class='layui-btn layui-btn-radius layui-btn-sm layui-btn-primary look'>" +
                    "<i class='layui-icon'>&#xe615;</i>查看反馈内容<span style='display:none;'>" + this.columns.content + "</span></button>" +
                    "</td>";
            }else{
                operation = "<td>" +
                    "<button class='layui-btn layui-btn-radius layui-btn-sm layui-btn-primary look'>" +
                    "<i class='layui-icon'>&#xe615;</i>查看反馈内容<span style='display:none;'>" + this.columns.content + "</span></button>" +
                    "<button class='layui-btn layui-btn-radius layui-btn-sm layui-btn-warm yes'>" +
                    "<i class='layui-icon'>&#xe618;</i>已查验</button>" +
                    "</td>";
            }
            // 赋给tbody
            $("<tr id='" + this.columns.id + "'></tr>").append(realNameTd).append(statusTd).append(createTimeTd).append(operation).appendTo("#content");
        });
    }

    //查验问题成功
    $(document).on("click", ".look", function () {
        //页面层
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['500px', '260px'], //宽高
            content: $(this).find("span").html()
        });
    });

    //下拉框筛选
    form.on('select(feedbackStatus)', function (data) {
        status = data.value;
        to_page(1, 10);
    });

    //查验问题成功
    $(document).on("click", ".yes", function () {
        var id = $(this).parent().parent().attr("id");
        layer.confirm("查验成功了吗?", {
            icon: 3,
            title: '温馨提示'
        }, function (index) {
            $.ajax({
                url: "/appfeedback/updAppFeedbackStatus",
                type: "post",
                data: {
                    "id": id
                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.status == 0) {
                        to_page(1, 10);
                    }
                }
            });
        });
    });

    function getMyDate(str) {
        var oDate = new Date(str),
            oYear = oDate.getFullYear(),
            oMonth = oDate.getMonth() + 1,
            oDay = oDate.getDate(),
            oHour = oDate.getHours(),
            oMin = oDate.getMinutes(),
            oSen = oDate.getSeconds(),
            oTime = oYear + '-' + getzf(oMonth) + '-' + getzf(oDay) + ' ' + getzf(oHour) + ':' + getzf(oMin) + ':' + getzf(oSen); //最后拼接时间
        return oTime;
    };

    //补0操作
    function getzf(num) {
        if (parseInt(num) < 10) {
            num = '0' + num;
        }
        return num;
    }
}();